import {
  getGoodsList
} from "../../../../api/goods"
Page({
  data: {
    goodsList: [], // 商品列表数据
    isFinish: false, // 判断数据是否加载完毕,
    requestData: {
      page: 1,
      limit: 10,
      category1Id: '',
      category2Id: '',
    },
    total: 0,
    flag: false,
  },
  // 返回上一页，查看其他商品
  gotoBack() {
    wx.navigateBack()
  },
  async getGoodsData({
    page,
    limit,
    ...Query
  }) {
    this.data.flag = true
    const {code,data} = await getGoodsList(page, limit, Query)
    if (code === 200) {
      this.setData({
        goodsList: [...this.data.goodsList, ...data.records],
        total: data.total,
        flag: false
      })
    } else {
      wx.showToast({
        title: '商品列表数据获取失败',
        icon: "error"
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getGoodsData(Object.assign(this.data.requestData, options))
  },
  onPullDownRefresh() {
    this.setData({
      "requestData.page": 1,
      "requestData.limit": 10,
      "goodsList":[],
      "total":0,
      "isFinish":false,
    })
    this.getGoodsData(this.data.requestData)
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // 如果true 表示有请求执行中,false表示没有请求
    // 在发送请求前设置为true, 发送请求之后设置为false
    if (this.data.flag) return
    if (this.data.goodsList.length == this.data.total) {
      this.setData({
        isFinish: true
      })
      return
    }
    this.setData({
      "requestData.page": this.data.requestData.page + 1,
    })
    this.getGoodsData(this.data.requestData)
  },
})